
package model.fileManager;

import java.io.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author FELIPE
 */
public class file {
    

        //Method that open a especified file and return the string contained in it
        public static String openFile(String fileName){
        
            File f=new File(fileName);
            
            if (f.exists()){
                
                try{
                
                    FileReader fr=new FileReader(f);
                    BufferedReader br =new BufferedReader(fr);  
                    
                    String lineTemp = br.readLine();
                    String line="";
                    

                    while (lineTemp != null) {

                        
                        line = line +("\r")+ lineTemp;//each time it reads the following line
                        lineTemp=br.readLine();
                    }
                    
                    return line;
                    
                
                }catch (IOException e){
                    JOptionPane.showMessageDialog(new JFrame(), e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
                    return null;
                }
            
            }
            else {
                JOptionPane.showMessageDialog(new JFrame(),"The file " + fileName + " does not exists","Error",JOptionPane.ERROR_MESSAGE);
                return null;
            }
        
        }//openFile
        
        
        //method that writes a file with a especified string
        public static void saveFile(String fileName,String fileContent){
            
            File file = new File(fileName);
            
            
            try {
                    
                Writer output = new BufferedWriter(new FileWriter(file));
                output.write(fileContent);
                output.close();

            } catch (IOException e) {

                JOptionPane.showMessageDialog(new JFrame(), e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
               
            } 
            
            
        }
    
}
